package com.ruoyi.api.service.impl;


import com.ruoyi.api.domain.WxLogin;
import com.ruoyi.api.mapper.WxLoginMapper;
import com.ruoyi.api.service.WxLoginService;
import com.ruoyi.common.utils.http.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WxLoginServiceImpl implements WxLoginService {
    @Autowired
    WxLoginMapper wxLoginMapper;

    /**
     * 查询当前登录用户是否授权微信公众号
     *
     * @param openId 查询条件
     * @return 结果
     */
    @Override
    public WxLogin selectWxLogin(String openId) {
        return wxLoginMapper.selectWxLogin(openId);
    }

    /**
     * @param code
     * @return
     */
    @Override
    public String sendResponseH5(String code) {
        Map<Object, Object> map = new HashMap<>();
        map.put("appid", "wx3d30d6d6ede0fd91");
        map.put("secret", "3a2150879a846e4c6d9a65eb8477a5d6");
        map.put("code", code);
        map.put("grant_type", "authorization_code");

        return HttpUtils.sendGet("https://api.weixin.qq.com/sns/oauth2/access_token", "appid=wx3d30d6d6ede0fd91" + "&secret=3a2150879a846e4c6d9a65eb8477a5d6" + "&code=" + code + "&grant_type=authorization_code");

    }

    @Override
    public int insertWxLogin(WxLogin wxLogin) {
        return wxLoginMapper.insertWxLogin(wxLogin);
    }

    @Override
    public void deleteWxLogin(String openId) {
        wxLoginMapper.deleteWxLogin(openId);
    }
}
